package com.study.aio.client;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wusz on 2018/5/17.
 */
public class Test implements Serializable {
    public String s = "hello";

    public static final long serialVersionUID = -4841994837142898086L;
    private List<String> list = new ArrayList<>();
    /* stash测试 */
    public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
//        testLatch();
//        bigDecimal();
//        Date date = new Date();
//        System.out.println(System.currentTimeMillis());
//        long l = System.currentTimeMillis();
//        Date date = new Date(l);
//        Date date1 = new Date(l);
//        System.out.println(date.equals(date1));
//        String tom = new String("Tom");
//        System.out.println(tom);
//        serialize();
//        fun();
        /*Test t1 = new Test();
        Test t2 = new Test();
        System.out.println(t1.equals(t2));
        List<Test> list = Arrays.asList(t1);
        boolean c = list.contains(t2);
        System.out.println(c);
        System.out.println(((Test)null).serialVersionUID);*/
//        try {
//            int i = 1 / 0;
//        } finally {
//            System.out.println("end");
//        }
//        System.out.println("-----"+System.getProperty("line.separator")+"------");
        /*ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.putInt(3);
        byteBuffer.position(0);
        byteBuffer.put("hello".getBytes());
        byteBuffer.flip();
        byte[] bytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(bytes);
        System.out.println(new String(bytes,"UTF-8"));*/
       /* List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
        list.remove(5L);
        System.out.println(list);*/
       /*List<String> list = Arrays.asList("a","b");
        String[] arr1 = list.stream().toArray(size -> {
            System.out.println(size);
            return new String[size];
        });
        String s = StringUtils.join(list, " ");
        System.out.println(s);*/
       /*double v = 1.4999;
        String s = new BigDecimal(v).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        System.out.println(s);*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        Date date1 = sdf.parse("2018-06-06 12:00:00");
        Date date2 = sdf.parse("2019-06-06 12:00:00");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int start = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(date2);
        int end = calendar.get(Calendar.DAY_OF_YEAR);
        long time = date2.getTime() - date1.getTime();
        if(start != end || time > 24 * 60 * 3600 * 1000) {
            System.out.println("error");
        }

    }


    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Test) {
            Test t = (Test) obj;
            if(t.s.equalsIgnoreCase(this.s)) {
                return true;
            }
        }
        return super.equals(obj);
    }

    private static void fun() {
        for(int i = 1; i<101;i++) {
            for(int j = 2; ; j++) {
                if(j >= i) {
                    System.out.println(i);
                    break;
                }
                if(i % j == 0) break;
            }
        }
    }

    @org.junit.Test
    public void candidate() {
        List<Integer> list = Arrays.asList(1, 2);
        listAll(list,"");
    }

    private void listAll(List<Integer> list, String s) {
        for(int i = 0; i< list.size();i++) {
            List<Integer> list1 = new LinkedList<>(list);
            String temp = s + list.get(i);
            System.out.println(temp);
            list1.remove(i);
            listAll(list1,temp);
        }
    }

    @org.junit.Test
    public void dserialize() throws IOException, ClassNotFoundException {
        try (ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.txt"))) {
            Test test = (Test)is.readObject();
            System.out.println(test.serialVersionUID);
            System.out.println(test.list);
        }
    }
    @org.junit.Test
    public  void serialize() throws IOException {
        Test test = new Test();
        System.out.println(test.serialVersionUID);
        test.list.addAll(Arrays.asList("a","b"));
        System.out.println(test.list);
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.txt"));
        os.writeObject(test);
        os.close();
    }

    private static void bigDecimal() {
        BigDecimal bigDecimal = new BigDecimal(1);
        BigDecimal bigDecimal1 = bigDecimal.setScale(1, BigDecimal.ROUND_DOWN);
        System.out.println(bigDecimal);
        System.out.println(bigDecimal1);
    }

    /*private static void testLatch() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);
        executor.execute(()->{
            try {
                System.out.println("睡眠中");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i=0;i<2;i++) {
                System.out.println("释放：" + i);
                latch.countDown();
            }
        });
        latch.await();
        System.out.println("end");
        executor.shutdown();
    }*/
}
